package cn.itsource.org.service.impl;

import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.utils.MD5Utils;
import cn.itsource.basic.utils.StrUtils;
import cn.itsource.org.domain.Logininfo;
import cn.itsource.org.domain.User;
import cn.itsource.org.domain.dto.SmsCodeParam;
import cn.itsource.org.mapper.LogininfoMapper;
import cn.itsource.org.mapper.UserMapper;
import cn.itsource.org.service.IUserService;
import cn.itsource.basic.base.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huang
 * @since 2022-07-09
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    public void register(SmsCodeParam param) {
//        1. 参数非空校验
        if(StringUtils.isEmpty(param.getPassword()) || StringUtils.isEmpty(param.getConfirmPassword())
        || StringUtils.isEmpty(param.getSmsCode()) ||StringUtils.isEmpty(param.getPhone())){
            throw new CustomException("参数异常");
        }
//        2. 两次密码是否输入一致
        if(!param.getPassword().equals(param.getConfirmPassword())){
            throw new CustomException("两次输入的密码不一致，请确认后提交");
        }
//        3. 校验用户是否存在
        User user = userMapper.loadByUserName(param.getPhone());
        if (user != null){
            throw new CustomException("用户已存在");
        }
//        4. 校验短信验证码是否存在
        Object smsCode = redisTemplate.opsForValue().get("register:" + param.getPhone());
//        1. 不存在：抛出自定义异常，短信验证码过期
        if(smsCode == null){
            throw new CustomException("短信验证码已过期，请重新发送");
        }
//        2. 存在：校验是否正确
        String[] split = smsCode.toString().split(":");
        if(!split[0].equals(param.getSmsCode())){
            throw new CustomException("验证码错误");
        }
//        5. 密码MD5加密，记录盐值
        String salt = StrUtils.getComplexRandomString(32);
        String md5Password = MD5Utils.encrypByMd5(param.getPassword() + salt);
//        6. 先设置默认值，保存login_info
        User newUser = dto2User(param,salt,md5Password);
        Logininfo linfo = user2Logininfo(newUser);
//        7. 再保存user
        logininfoMapper.insert(linfo);
        newUser.setLogininfoId(linfo.getId());
        userMapper.insert(newUser);
    }

    private Logininfo user2Logininfo(User newUser) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(newUser,logininfo);
        logininfo.setDisable(1);
        logininfo.setType(1);
        return logininfo;
    }

    private User dto2User(SmsCodeParam param,String salt,String md5Password) {
        User user = new User();
        user.setUsername(param.getPhone());
        user.setPhone(param.getPhone());
        user.setSalt(salt);
        user.setPassword(md5Password);
        user.setState(1);
        user.setCreatetime(new Date());
        return user;
    }
}
